package org.cst.activity;



import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;

import android.R.integer;
import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends Activity {

	EditText username;
	EditText password;
	EditText email;
	EditText gerder;
	EditText serverip;
	EditText serverpor;
	Button register;	

	//服务器server/IP地址
	//private final String ADDRESS = "58.39.102.180";
	private final String ADDRESS="192.168.0.3";
	//服务器端口
	private final int PORT=6001;
	public Socket socker;
	//用于创建的socket读写
	DataInputStream dis = null;
	DataOutputStream dos = null;
	//用于服务器注册返回
	String content;
	//boolean RegSatus=false;
	//显示是否注册成功，及注册不成功原因。
	public Handler registerHandler= new Handler(){
		  public void handleMessage(Message msg) {    
	            super.handleMessage(msg); 
	            
	           //echo receive register content;
	            email.setText(content);    	            
	           // if("c02&f=0".equals(content)){
	            if(content.substring(0,7).equals("c02&f=0")){
	            	Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_LONG).show();
	            	//RegSatus=true;
	            }else  {
	            	Toast.makeText(RegisterActivity.this, "帐号已被注册", Toast.LENGTH_LONG).show();
				}
	            	
	        }    
	};
	
	
	
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		initViews();
	}
	
	
	public void initViews()
	{
		username=(EditText) findViewById(R.id.usernameRegister);
		password=(EditText) findViewById(R.id.passwordRegister);
		email=(EditText)findViewById(R.id.emailRegister);
		serverip=(EditText)findViewById(R.id.ServerIp);
		serverpor=(EditText)findViewById(R.id.ServerPort);
		register=(Button) findViewById(R.id.Register);
		
	
		
		register.setOnClickListener(new OnClickListener() {
			
    			@Override
			public void onClick(View v) {
				if(null!=username&&null!=password&&null!=email)
				{   //注册信息判断
					String name=username.getText().toString();
					String pass=password.getText().toString();
					String mail=email.getText().toString();
					final String ip=serverip.getText().toString();
					//int port=serverpor.getText().
					final int port=Integer.valueOf(serverpor.getText().toString());
					final String RegisterCmd="register"+"&name="+name+"&password="+pass+"&gerder=man"+"&qq="+mail;
					if("".equals(name.trim())&&"".equals(pass.trim())&&"".equals(mail.trim()))
					{
						Log.i("TAG","请正确输入");
						Toast.makeText(RegisterActivity.this, "请正确输入", Toast.LENGTH_LONG).show();
						return;
					}	
					//与服务器通信
					
					// 避免出现android.os.NetworkOnMainThreadException异常
					Thread registerThread = new Thread(){
						public void run() {
							try {
								//socker = new Socket(ADDRESS, PORT);
								socker=new Socket(ip,port);
								dis = new DataInputStream(socker.getInputStream());
							    dos = new DataOutputStream(socker.getOutputStream());
							    dos.writeBytes(RegisterCmd);
							    byte[] temp = new byte[30];					
								if (socker.isConnected()){					
									if(dis.read(temp)>0){
										 content=new String(temp);
										 content=String.copyValueOf(content.toCharArray(), 0, content.length());
									     //content += "\n";  		  
										 registerHandler.sendMessage(registerHandler.obtainMessage());
									    
									}
									
								
									socker.close();
									sleep(1000000);
								}else {
									Log.e("Register", "socke disconnect!");
									Toast.makeText(RegisterActivity.this, "获取服务器列表失败", Toast.LENGTH_LONG).show();
								}
									
							} catch (Exception e) {
								// TODO: handle exception
							}
							
						
							//if (socker.isConnected()) {
							//	Toast.makeText(RegisterActivity.this, "获取服务器列表成功", Toast.LENGTH_LONG).show();
							//}
						  	//System.exit(1);	
							
						}
		
					};
					registerThread.start();
					//.start();
					
					//while(RegSatus);
				    //try {
				    //  socker = new Socket(ADDRESS, PORT);	
				    //	dis = new DataInputStream(RegisterSocket.getInputStream());
					//	dos = new DataOutputStream(RegisterSocket.getOutputStream());
				    //	dos.writeBytes(RegisterCmd);
				    //	socket = new Socket(ADDRESS, PORT);
					//} catch (Exception e) {
					//	e.printStackTrace();
					//	Toast.makeText(RegisterActivity.this, "获取服务器列表异常0", Toast.LENGTH_LONG).show();								
					}
				}
		});
	}
}
